<?php

namespace app\user\controller\admin;

use sheep\controller\Api;
use GuzzleHttp\Client;
class Demo extends Api
{
    protected $baseUrl = 'http://cutepet.liuliangbenben.com';

    public function login()
    {
        $url = '/admin/index/login';
        $client = new Client([
            'base_uri' => $this->baseUrl,
            'verify' => false,
        ]);
        $data = [
            'account' => 'root',
            'password' => 'CrzBDsFV3t.'
        ];

        $headers = [
            'Content-Type' => 'application/x-www-form-urlencoded'
        ];
        $options = [
            'headers' => $headers,
            'form_params' => $data,
        ];
        try {
            $response = $client->post($url, $options);
            $result = json_decode($response->getBody()->getContents(), true);
            if ($result['error'] == 0) {
                $Authorization = $response->getHeaders()['Authorization'];
                return $Authorization[0];
            }
            return false;
        } catch (\Exception $e) {
            // 记录详细错误信息
            echo "Request failed: " . $e->getMessage() . "\n";
            if ($e->hasResponse()) {
                echo "Response: " . $e->getResponse()->getBody()->getContents() . "\n";
            }
        }
    }
}